from flask import request
from subsidy.app.utils.JwtUtils import *
from subsidy.app.pojo.Result import ResultError
from subsidy.app.config.Config import pattentyUrl
import re

def checkUrl(path):
    if path in pattentyUrl:
        return False
    pattern = "^/user/.*"
    pathStatus = re.match(pattern, path)
    if pathStatus:
        return True
    else:
        return False

def checkTokenRequest():
    path = request.path
    if request.path == "/":
        # return render_template('login.html')
        print(f"访问了: {request.path}")

    elif checkUrl(path):
        if 'Authorization' not in request.headers:
            return ResultError(401, "没有授权!")

        status, userInfo = parse_jwt(request.headers.get("Authorization"))

        if not status:
            return ResultError(401, "授权失败")
